package cn.yj.common.interfaces;

import cn.yj.common.emums.PayFrom;
import cn.yj.common.entity.RefundQueryParams;
import cn.yj.common.monitor.IPayOrderMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author 永健
 * @since 2021-09-22 16:48
 */
public abstract class AbstractPay<T, R> implements ApplicationContextAware, IPay<T, R> {

    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    private static ApplicationContext app;

    protected IPayOrderMonitor iPayOrderMonitor;

    protected void setIPayOrderMonitor(IPayOrderMonitor payOrderMonitor) {
        this.iPayOrderMonitor = payOrderMonitor;
    }

    protected void addPayOrderMonitor(long expireTime, String outTradeNo, AbstractPay abstractPay) {
        try {
            // 加多11秒中轮训多一次
            iPayOrderMonitor.payMonitor(expireTime + (11 * 1000), outTradeNo, abstractPay);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void addRefundOrderMonitor(RefundQueryParams params, AbstractPay abstractPay) {
        try {
            iPayOrderMonitor.refundMonitor(params, abstractPay);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        app = applicationContext;
    }

    protected ApplicationContext getApplicationContext() {
        return app;
    }

    public abstract boolean support(PayFrom payFrom);

}
